package handlers

import (
	"ecom-master/cache"
	"ecom-master/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func ShopAdd(c *gin.Context) {
	shop_code := c.PostForm("shop_code")
	shop_price, _ := strconv.Atoi(c.PostForm("shop_price"))
	shop_num, _ := strconv.Atoi(c.PostForm("shop_num"))

	data := models.Shop{
		ShopCode:  shop_code,
		ShopPrice: shop_price,
		ShopNum:   shop_num,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	order_name := c.PostForm("order_name")
	sid, _ := strconv.Atoi(c.PostForm("sid"))

	data := models.Order{
		OrderNo:    order_no,
		OrderName:  order_name,
		OrderPrice: order_price,
		Sid:        sid,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	var orderList []models.OrderList

	/*
			SELECT
		  orders.id as OrderId,
		  shops.shop_code as ShopCode,
		  shops.shop_price as ShopPrice,
		  shops.shop_num as ShopNum,
		  orders.order_no as OrderNo,
		  orders.order_name as OrderName,
		  orders.order_price as OrderPrice,
		  orders.sid as Sid
		FROM
		  shops
		  LEFT JOIN orders ON shops.id = orders.sid
	*/
	redis_key := "order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("shops").
			Select(
				"orders.id as OrderId",
				"shops.shop_code as ShopCode",
				"shops.shop_price as ShopPrice",
				"shops.shop_num as ShopNum",
				"orders.order_no as OrderNo",
				"orders.order_name as OrderName",
				"orders.order_price as OrderPrice",
				"orders.sid as Sid").
			Joins("LEFT JOIN orders ON shops.id = orders.sid").
			Order("OrderId asc").
			Offset(Offset).
			Limit(size).
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  orderList,
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	var orderList models.OrderList
	/*
		SELECT
		  orders.id as OrderId,
		  shops.shop_code as ShopCode,
		  shops.shop_price as ShopPrice,
		  shops.shop_num as ShopNum,
		  orders.order_no as OrderNo,
		  orders.order_name as OrderName,
		  orders.order_price as OrderPrice,
		  orders.sid as Sid
		FROM
		  shops
		  LEFT JOIN orders ON shops.id = orders.sid
		WHERE orders.id=1
	*/
	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("shops").
			Select(
				"orders.id as OrderId",
				"shops.shop_code as ShopCode",
				"shops.shop_price as ShopPrice",
				"shops.shop_num as ShopNum",
				"orders.order_no as OrderNo",
				"orders.order_name as OrderName",
				"orders.order_price as OrderPrice",
				"orders.sid as Sid").
			Joins("LEFT JOIN orders ON shops.id = orders.sid").
			Order("OrderId asc").
			Where("orders.id=?", id).
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": orderList,
	})
	return
}
